package cn.ljl.user.service.impl;

import cn.ljl.basic.exception.BusinessException;
import cn.ljl.basic.util.Md5Utils;
import cn.ljl.basic.util.StrUtils;
import cn.ljl.user.domain.Logininfo;
import cn.ljl.user.domain.User;
import cn.ljl.user.mapper.LogininfoMapper;
import cn.ljl.user.mapper.UserMapper;
import cn.ljl.user.service.IUserService;
import cn.ljl.basic.service.impl.BaseServiceImpl;
import net.bytebuddy.asm.Advice;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 业务实现类：
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void phoneRegister(Map<String, String> map) {
        String phone = map.get("phone");
        String phoneCode = map.get("phoneCode");
        String pwd = map.get("pwd");
        String rePwd = map.get("rePwd");
        //一：校验
        //1.空值
        if(StringUtils.isEmpty(phone) || StringUtils.isEmpty(phoneCode)
        || StringUtils.isEmpty(pwd) || StringUtils.isEmpty(rePwd)){
            throw new BusinessException("信息不能为空!!!");
        }
        //2.密码是否一致
        if(!pwd.equals(rePwd)){
            throw new BusinessException("两次密码不一致!!!");
        }
        //3.手机验证码是否过期，是否正确
        Object obj = redisTemplate.opsForValue().get("register:" + phone);
        if(obj==null){
            throw new BusinessException("手机验证码过期!!!");
        }
        //redis中手机验证码的存储格式【业务键:手机号码 - 验证码:时间戳】
        if(!phoneCode.equals(obj.toString().split(":")[0])){
            throw new BusinessException("手机验证码错误!!!");
        }
        //二：添加到数据库
        //当将一个对象转换成另外一个对象的时候，而且代码量的时候就用obj2Obj
        User user = map2User(map);
        Logininfo logininfo = user2Logininfo(user);
        //1.先添加t_logininfo
        logininfoMapper.save(logininfo);
        //2.再添加t_user
        user.setLogininfoId(logininfo.getId());
        userMapper.save(user);
    }

    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        //根据属性同名原则：
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);//用户手机号注册
        logininfo.setDisable(true);//手机注册默认启用
        return logininfo;
    }

    private User map2User(Map<String, String> map) {
        User user = new User();
        String phone = map.get("phone");
        user.setUsername(phone);
        user.setPhone(phone);
        //生成32位随机盐值
        String salt = StrUtils.getComplexRandomString(32);
        //加密加盐
        String md5Pwd = Md5Utils.encrypByMd5(map.get("pwd") + salt);
        user.setSalt(salt);
        user.setPassword(md5Pwd);
        user.setState(1);
        return user;
    }
}
